package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsAttributeMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.service.DtsGoodsAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DtsGoodsAttributeServiceImpl implements DtsGoodsAttributeService {

    @Autowired
    DtsGoodsAttributeMapper goodsAttributeMapper;

    @Override
    public void update(Integer id, DtsGoodsAttribute[] attributes) {
        LambdaQueryWrapper<DtsGoodsAttribute> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsAttribute::getGoodsId,id);
        goodsAttributeMapper.delete(qw);

        if(attributes != null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                attribute.setAddTime(new Date());
                attribute.setUpdateTime(new Date());
                attribute.setDeleted(false);
                attribute.setGoodsId(id);
                goodsAttributeMapper.insert(attribute);
            }
        }
    }

    @Override
    public void delete(Integer id) {
        DtsGoodsAttribute goodsAttribute = new DtsGoodsAttribute();
        goodsAttribute.setDeleted(false);
        LambdaQueryWrapper<DtsGoodsAttribute> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsAttribute::getGoodsId,id);
        goodsAttributeMapper.update(goodsAttribute,qw);
    }

    @Override
    public void insert(Integer id, DtsGoodsAttribute[] attributes) {
        if(attributes != null && attributes.length>0){
            for (DtsGoodsAttribute attribute : attributes) {
                attribute.setDeleted(false);
                attribute.setGoodsId(id);
                attribute.setUpdateTime(new Date());
                attribute.setAddTime(new Date());
                goodsAttributeMapper.insert(attribute);
            }
        }
    }
}
